<?php
/**
 * Created by PhpStorm.
 * User: dingboxu
 * Date: 17/2/21
 * Time: 下午5:07
 */

use Workerman\Protocols\Http;

require_once __DIR__ . '/../../Workerman/Autoloader.php';

require_once __DIR__.'/Config/Const.php';


try {

    // 获取前台传入的数据
    $handle = fopen ( 'php://input', 'r' );
    $json = fgets ( $handle );
    echo "$json";
    $data_arr = json_decode ( $json, true );


//    // 行为日志记录
//    Util_log::action_log(__FILE__, '001', $action_type);
//    //调式日志
//    Util_log::debug(__FILE__, 'DEBUG');
//    //错误日志
//    Util_log::error(__FILE__, 'ERR');


    $dataDictionary = $data_arr[DATA_DICTIONARY];

    if (array_key_exists(REQUEST_ID,$data_arr)) {
        $requestId = $data_arr[REQUEST_ID];
    } else {
        $requestId = 0;
    }

    \Workerman\Protocols\Http::header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    \Workerman\Protocols\Http::header("Access-Control-Allow-Credentials:true");

    Http::sessionStart();

    switch($data_arr[SYS_EVENT]) {

        case EVENT_SPECIES_LIST:
            // 首页获取花木品种列表
            new \ActionsHttp\SpeciesList(EVENT_SPECIES_LIST,$requestId,$dataDictionary,$connection);
            break;

        case EVENT_GOURP_LIST:
            // 获取花木品种分组列表
            echo "JIjoi";
            new \ActionsHttp\GroupList(EVENT_GOURP_LIST,$requestId,$dataDictionary,$connection);
            break;

        case EVENT_PRODUCTOR_LIST:
            // 获取分组内花木品种例表
            new \ActionsHttp\ProductorList(EVENT_PRODUCTOR_LIST,$requestId,$dataDictionary,$connection);
            break;

        case EVENT_PRODUCTOR_INFO:
            // 获取分组内花木品种例表
            new \ActionsHttp\ProductorInfo(EVENT_PRODUCTOR_INFO,$requestId,$dataDictionary,$connection);
            break;

        default:
            $connection->send(\Actions\ActionUtils::getResponseData(array(),$data_arr[SYS_EVENT],$requestId,RESULT_CODE_NOT_FOUND,"没有找到该通讯"));
            break;

    }
} catch ( Exception $e ) {
    // 记录抛出的异常信息
    $err_msg = $e->getMessage ();
    $err_file = $e->getFile ();
    $err_line = $e->getLine ();
    $msg = "错误信息:$err_msg! $err_file,第 $err_line 行";
    Util_log::error ( __FILE__, $msg );
    // 通知前台发生异常
    $result = array (
        RESULT => '2',
        ERR_MSG => '预料外的错误！'
    );
}

